package com.ljh.bigevent.controller;

import com.ljh.bigevent.pojo.Result;
import com.ljh.bigevent.pojo.User;
import com.ljh.bigevent.service.UserService;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Validated
@RestController
@RequestMapping("/user")
@Tag(name="用户模块")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
        @Operation(summary = "注册接口")
    public Result register(@Pattern(regexp="^\\S{5,16}$") String username,
                           @Pattern(regexp="^\\S{5,16}$") String password) {
        return userService.register(username, password);
    }

    @PostMapping("/login")
        @Operation(summary = "登录接口")

    public Result<String> login(@Pattern(regexp="^\\S{5,16}$") String username,
                                @Pattern(regexp="^\\S{5,16}$") String password) {
        return userService.login(username, password);
    }

    @GetMapping("/userInfo")
        @Operation(summary = "获取用户信息接口")

    public Result<User> userInfo() {
        return userService.userInfo();
    }

    @PutMapping("/update")
        @Operation(summary = "用户信息更新接口")
    public Result update(@RequestBody @Validated User user) {
        return userService.update(user);
    }

    @PatchMapping("/updateAvatar")
        @Operation(summary = "更新用户图片接口")

    public Result updateAvatar(@RequestParam @URL String avatarUrl) {
        return userService.updateAvatar(avatarUrl);
    }

    @PatchMapping("/updatePwd")
        @Operation(summary = "更新用户密码接口")

    public Result updatePwd(@RequestBody Map<String,String> params,
                            @RequestHeader(name="Authorization") String token) {
        return userService.updatePwd(params, token);
    }
}